package com.yuan.model_community.service.impl;

import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_community.domain.CommunityBuilding;
import com.yuan.model_community.repository.CommunityBuildingDao;
import com.yuan.model_community.service.CommunityBuildingService;
import com.yuan.model_community.service.dto.CommunityBuildingDto;
import com.yuan.model_community.service.mapstruct.CommunityBuildingMapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;


@Service
@RequiredArgsConstructor
public class CommunityBuildingServiceImpl implements CommunityBuildingService {

    private final CommunityBuildingDao communityBuildingDao;

    private final CommunityBuildingMapper communityBuildingMapper;

    @Override
    public PageResult<CommunityBuildingDto> queryAll(CommonQueryCriteria criteria, Pageable pageable) {
        Page<CommunityBuilding> page = communityBuildingDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(communityBuildingMapper::toDto));
    }

    @Override
    public List<CommunityBuildingDto> queryAll() {
        List<CommunityBuilding> all = communityBuildingDao.findAll();
        return communityBuildingMapper.toDto(all);
    }

    @Override
    public List<CommunityBuildingDto> queryAll(CommonQueryCriteria criteria) {
        List<CommunityBuilding> users = communityBuildingDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        return communityBuildingMapper.toDto(users);
    }

    @Override
    public void download(List<CommunityBuildingDto> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CommunityBuildingDto communityInfoDto : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(CommunityBuilding resources) {
        communityBuildingDao.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CommunityBuilding resources) throws Exception {
        Optional<CommunityBuilding> byId = communityBuildingDao.findById(resources.getBuildingId());
        if (byId.isPresent()) {
            BeanUtils.copyProperties(resources, byId.get());
            communityBuildingDao.save(byId.get());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        communityBuildingDao.deleteById(ids);
    }
}
